<template>
    <div class="main-content">
      <div class="data-content">
        <div class="table-content">
          <div class="table-title">
            <div class="title-text">教师列表</div>
            <div class="title-btn">
              <el-button type="primary" @click="addClick">新增</el-button>
              <el-button type="danger" @click="delClick()" :disabled="selRows.length <= 0">批量删除</el-button>
            </div>
          </div>
          <el-table
            :data="dataList"
            border
            v-loading="dataListLoading"
            @selection-change="selectionChangeHandler"
            style="width: 100%;">
            <el-table-column
              type="selection"
              header-align="center"
              align="center"
              width="50">
            </el-table-column>
            <el-table-column
              prop="gonghao"
              header-align="center"
              align="center"
              label="工号">
            </el-table-column>
            <el-table-column
              prop="laoshixingming"
              header-align="center"
              align="center"
              label="教师姓名">
            </el-table-column>
            <el-table-column
              prop="touxiang"
              header-align="center"
              align="center"
              label="头像">
              <template #default="scope">
                <el-image v-if="scope.row.touxiang.substring(0,4)=='http'" preview-teleported
                          :preview-src-list="[scope.row.touxiang.split(',')[0]]"
                          :src="scope.row.touxiang.split(',')[0]" style="width:100px;height:100px"></el-image>
                <el-image v-else preview-teleported
                          :preview-src-list="[$config.url+scope.row.touxiang.split(',')[0]]"
                          :src="$config.url+scope.row.touxiang.split(',')[0]" style="width:100px;height:100px">
                </el-image>
              </template>
            </el-table-column>
            <el-table-column
              prop="xingbie"
              header-align="center"
              align="center"
              label="性别">
            </el-table-column>
            <el-table-column
              prop="mobile"
              header-align="center"
              align="center"
              label="手机号">
            </el-table-column>
            <el-table-column
              prop="banji"
              header-align="center"
              align="center"
              label="所属班级">
            </el-table-column>
            <el-table-column
              fixed="right"
              header-align="center"
              align="center"
              width="150"
              label="操作">
              <template #default="scope">
                <el-button type="text" size="small" @click="editClick(scope.row.id)">修改</el-button>
                <el-button type="text" size="small" @click="delClick(scope.row.id)">删除</el-button>
              </template>
            </el-table-column>
          </el-table>
          <el-pagination
            @size-change="sizeChangeHandle"
            @current-change="currentChangeHandle"
            :current-page="pageIndex"
            :page-sizes="[10, 20, 50, 100]"
            :page-size="pageSize"
            :total="totalPage"
            layout="total, sizes, prev, pager, next, jumper"
            class="pagination-content">
          </el-pagination>
        </div>
      </div>
      <formModel ref="formRef" @formModelChange="formModelChange"></formModel>
    </div>
  </template>
  
  <script>
  import formModel from './formModel.vue'
  
  export default {
    components: {
      formModel
    },
    data () {
      return {
        dataList: [],
        pageIndex: 1,
        pageSize: 10,
        totalPage: 0,
        dataListLoading: false,
        selRows: []
      }
    },
    activated () {
      this.getDataList()
    },
    created() {
      this.getDataList()

    },
    methods: {
      // 获取数据列表
      getDataList () {
        console.log("getDataList")
        this.dataListLoading = true
        let params = {
          'page': this.pageIndex,
          'limit': this.pageSize,
          'sort': 'id',
          'order': 'desc'
        }
        this.$http({
          url: '/laoshi/list',
          method: 'get',
          params: params
        }).then(({data}) => {
          if (data && data.code === 0) {
            this.dataList = data.page.list
            this.totalPage = data.page.totalCount
          } else {
            this.dataList = []
            this.totalPage = 0
          }
          this.dataListLoading = false
        })
      },
      // 每页数
      sizeChangeHandle (val) {
        this.pageSize = val
        this.pageIndex = 1
        this.getDataList()
      },
      // 当前页
      currentChangeHandle (val) {
        this.pageIndex = val
        this.getDataList()
      },
      // 多选
      selectionChangeHandler (val) {
        this.selRows = val
      },
      // 新增
      addClick () {
        this.$refs.formRef.init()
      },
      // 修改
      editClick (id) {
        this.$refs.formRef.init(id, 'edit')
      },
      // 删除
      delClick (id) {
        var ids = id ? [id] : this.selRows.map(item => {
          return item.id
        })
        this.$confirm(`确定对[id=${ids.join(',')}]进行[${id ? '删除' : '批量删除'}]操作?`, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.$http({
            url: '/laoshi/delete',
            method: 'post',
            data: ids
          }).then(({data}) => {
            if (data && data.code === 0) {
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 1500,
                onClose: () => {
                  this.getDataList()
                }
              })
            } else {
              this.$message.error(data.msg)
            }
          })
        })
      },
      // 表单提交成功回调
      formModelChange () {
        this.getDataList()
      }
    }
  }
  </script>
  
  <style lang="scss" scoped>
  .main-content {
    padding: 20px;
    .data-content {
      background-color: #fff;
      border-radius: 4px;
      padding: 20px;
      .table-content {
        .table-title {
          display: flex;
          justify-content: space-between;
          align-items: center;
          margin-bottom: 20px;
          .title-text {
            font-size: 18px;
            font-weight: bold;
          }
        }
        .pagination-content {
          margin-top: 20px;
          text-align: right;
        }
      }
    }
  }
  </style>